package com.beimihu.leetcode;

import java.util.HashMap;
import java.util.Map;

public class ManyElements {
    private Map<Integer, Integer> countNums(int[] nums) {

        Map<Integer,Integer> counts = new HashMap<Integer,Integer>();

        for (int num : nums){
            if (!counts.containsKey(num)){
                counts.put(num,1);
            }
            else {
                counts.put(num,counts.get(num)+1);
            }
        }

        return counts;
    }
    public int majorityElement(int[] nums) {
        Map<Integer,Integer> counts = countNums(nums);

        int max = 0;
        int count = 0;

        for (Map.Entry<Integer, Integer> entry : counts.entrySet()) {
            Integer key = entry.getKey();
            Integer value = entry.getValue();
            if(value > count){
                count = value;
                max = key;
            }
        }

        return max;


    }

}
